package com.qinaxiwit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qianxi.wit.pojo.TbConsult;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianxi.wit.pojo.TbDoctor;
import com.qianxi.wit.pojo.TbFaculty;
import com.qianxi.wit.pojo.TbHospital;
import com.qianxi.wit.util.IdWorker;
import com.qinaxiwit.mapper.TbConsultMapper;
import com.qinaxiwit.mapper.TbDoctorMapper;
import com.qinaxiwit.mapper.TbFacultyMapper;
import com.qinaxiwit.mapper.TbHospitalMapper;
import com.qinaxiwit.service.TbConsultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zqp
 * @since 2021-01-14
 */
@Service
public class TbConsultServiceImpl extends ServiceImpl<TbConsultMapper, TbConsult> implements TbConsultService {

    @Autowired
    private TbConsultMapper consultMapper;
    @Autowired
    private TbDoctorMapper doctorMapper;
    @Autowired
    private TbHospitalMapper hospitalMapper;
    @Autowired
    private TbFacultyMapper facultyMapper;


    @Override
    public TbConsult insert(TbConsult consult) {
        String l = String.valueOf(new IdWorker().nextId());
        consult.setId(l);
        consult.setConsultCreateTime(new Date());
        System.out.println(consult);
        consultMapper.insert(consult);

        TbConsult tbConsult = consultMapper.selectById(l);
        return tbConsult;
    }

    @Override
    public Page<TbDoctor> findbydoctor(int page, int size, Long[] ids, TbDoctor doctor) {
//        Page<TbDoctor> page1 = new Page<>(page,size);
//
//        QueryWrapper<TbDoctor> wrapper = new QueryWrapper<>();
//
//        wrapper.in("doctor_id",ids);
//
//        if(!"".equals(doctor.getDoctorName())){
//            wrapper.like("doctor_name",doctor.getDoctorName());
//        }
//
//        doctorMapper.selectPage(page1,wrapper);
//  查询医院
        QueryWrapper<TbHospital> wrapper = new QueryWrapper<>();
        wrapper.like("hospital_name",doctor.getDoctorName());
        List<TbHospital> tbHospitals = hospitalMapper.selectList(wrapper);

        String hid = "";
        if(null != tbHospitals && tbHospitals.size() > 0){
            for (TbHospital tbHospital : tbHospitals) {
                hid += tbHospital.getHospitalId()+",";
            }
        }
//查询科室
        QueryWrapper<TbFaculty> wrapper1 = new QueryWrapper<>();
        wrapper.like("faculty_name",doctor.getDoctorName());
        List<TbFaculty> tbFaculties = facultyMapper.selectList(wrapper1);
        String fid = "";
        if(null != tbFaculties && tbFaculties.size() > 0){
            for (TbFaculty tbFaculty : tbFaculties) {
                fid += tbFaculty.getFacultyId()+",";
            }
        }

        //查医生
        QueryWrapper<TbDoctor> wrapper2 = new QueryWrapper<>();
        wrapper2.like("doctor_name",doctor.getDoctorName());
        List<TbDoctor> tbDoctors = doctorMapper.selectList(wrapper2);
        String a = "";
        for (int i=0; i<ids.length;i++){
            a += ids[i]+",";
        }
        if(null != tbDoctors && tbDoctors.size() > 0){
            for (TbDoctor tbDoctor : tbDoctors) {
                boolean flag = false;
                for (int i=0; i<ids.length;i++){
                    if(tbDoctor.getDoctorId()==ids[i]){
                        flag=true;
                    }
                }
                if(!flag){
                    a += tbDoctor.getDoctorId()+",";
                }
            }
        }

        a=a.substring(0,a.length()-1);
        hid = hid.substring(0,hid.length()-1);
        fid = fid.substring(0,fid.length()-1);
        Page<TbDoctor> page1 = new Page<>();
        List<TbDoctor> dlist = doctorMapper.findbydoctor(doctor.getDoctorName(),a,hid,fid,page-1,size);
        page1.setRecords(dlist);
        page1.setCurrent(page);
        page1.setSize(size);
        page1.setTotal(dlist.size());

        return page1;
    }

    @Override
    public List<TbConsult> selectLIst(String patientName) {
        QueryWrapper<TbConsult> wrapper = new QueryWrapper<>();
        wrapper.eq("patient_name",patientName);
        List<TbConsult> list = consultMapper.selectList(wrapper);
        return list;
    }
}
